import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

import Layout from '@/views/layout/Layout.vue'

Vue.use(VueRouter)

const routes = [
    {path: '/', name: 'Index', redirect: '/sys/namespace'},
    {path: '/login', name: 'Login', component: () => import('@/views/Login.vue')},
    {path: '/404', name:'NotFound', component: () => import('@/views/404.vue')},
    {
        path: '/', name: 'Layout', component: Layout,
        children: [
            {
              path: '/sys/namespace', name: 'Namespace', component: () => import('@/views/namespace/Index.vue')
            },
            {
              path: '/sys/router', name: 'Router', component: () => import('@/views/router/Index.vue')
            },
            {
              path: '/sys/user', name: 'User', component: () => import('@/views/user/Index.vue')
            }
        ]
    },
    {path: '*', redirect: '/404'}
]

const router = new VueRouter({
  routes
})


const noauths = new Set(['/login'])

router.beforeEach((to, from, next) => {

    let sid = store.getters.sid

    if(sid) {
        if('/login' == to.path) {
            next('/')
            return 
        }

        next()
        return
    }

    let flag = noauths.has(to.path)

    if(flag) {
        next()
        return 
    }

    next('/login')

})

export default router
